import java.awt.*;
import java.util.*;
import javax.swing.BoxLayout;

public class GraphOwner extends Panel implements Tickable {   
    // Graph displays
    private Scroll2DGraph velGraph, accelGraph;

    // Car data source
    private Core carDisplay;
    
    public GraphOwner(Core carDisplay) {
        this.carDisplay = carDisplay;
        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        this.setupGraphs();
    }
    
    public Insets getInsets() {
    	return new Insets(5, 1, 1, 5);
    }
    
    private void setupGraphs() {
    	// Establish layout/display of graphing components
        this.velGraph = new Scroll2DGraph("Velocity (m/s)", "Time (ticks)", 0, 20);
        this.accelGraph = new Scroll2DGraph("Acceleration (m/s^2)", "Time (ticks)", -4, 4);
        
        this.velGraph.setDisplayExtraXValues(true);
        this.accelGraph.setDisplayExtraXValues(true);
        
        int numCars = this.carDisplay.getNumCars();
        for (int i = 0; i < numCars; i++) {
        	Car currCar = this.carDisplay.getCar(i);
        	String name = "Car " + i + " - " + currCar.getColor().toString();
        	this.velGraph.addGraphDataLine(name, currCar.getColor(), currCar.getColor());
        	this.accelGraph.addGraphDataLine(name, currCar.getColor(), currCar.getColor());
        }

        this.add( velGraph );
        this.add( accelGraph );
    }
    
    public void tick() {
        // tick the graph tracking velocity and acceleration
        int numCars = this.carDisplay.getNumCars();
        for (int i = 0; i < numCars; i++) {
        	Car currCar = this.carDisplay.getCar(i);
        	String name = "Car " + i + " - " + currCar.getColor().toString();
        	this.velGraph.tick(  name, currCar.getVelocity() );
        	this.accelGraph.tick(name, currCar.getAcceleration()); 
        }
        this.velGraph.incrementTimer();
        this.accelGraph.incrementTimer();
    }
}
